﻿using System;
using System.Windows.Forms;

namespace DA6502.Controls
{
    public partial class DlgJumpTo : Form
    {
        const byte ENTER_KEY_CODE = 0x0D;
        public DlgJumpTo()
        {
            InitializeComponent();
        }

        private void BtnDlgJumpToOK_Click(object sender, EventArgs e)
        {
            string t = txtJumpTo.Text;
            ushort v = 0x0000;
            try
            {
                v = Convert.ToUInt16(t, 16);
            }
            catch (ArgumentException)
            { }
            catch (FormatException)
            {
                MessageBox.Show("P的值应为16进制数字");
            }
            catch (OverflowException)
            {
                MessageBox.Show("P的值应不大于0xFFFF");
            }
            if (JumpToAddress != null) JumpToAddress(v);
            txtJumpTo.Text = v.ToString("X4");
            Hide();
        }

        public delegate void JumpToAddressHandler(ushort addr);
        public event JumpToAddressHandler JumpToAddress;

        private void BtnDlgJumpToCancel_Click(object sender, EventArgs e)
        {
            txtJumpTo.Text = String.Empty;
            Hide();
        }

        private void TxtBaseAddr_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == ENTER_KEY_CODE) BtnDlgJumpToOK_Click(sender, e);
        }
    }
}
